﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    public partial class JCCD_Query_JC : BasePage
    {
        protected string DJ_Name;//单据类型
        protected string MX_URL;//明细页面URL
        protected string Edit_URL;//新增、修改页面URL
        private int DJ_Type
        {
            get { return Convert.ToInt32(this.GetUrlParam("lx")); }
        }
        private int DjStatus
        {
            get { return this.ViewState["DjStatus"] == null ? 0 : Convert.ToInt32(this.ViewState["DjStatus"]); }
            set { this.ViewState["DjStatus"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFirst();
                BindData();
            }
        }

        private void BindFirst()
        {
            if (DJ_Type == 0)
            {
                DJ_Name = "物料";
                MX_URL = "JCCDMX_JC_WL.aspx";
                Edit_URL = "JCCD_EditJC_WL.aspx";
            }
            else
            {
                DJ_Name = "包装箱";
                MX_URL = "JCCDMX_JC_BZX.aspx";
                Edit_URL = "JCCD_EditJC_BZX.aspx";
            }
            hyplAdd.NavigateUrl = Edit_URL;
            this.AspNetPager1.PageSize = GridView1.PageSize;
            BindDepartment(this.ddlDepartments, EnumDepartment.QualityControl, "请选择");
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            BindData();
        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            DjStatus = Convert.ToInt32(e.CommandArgument);
            this.AspNetPager1.CurrentPageIndex = 1;
            GridView1.Columns[8].Visible = DjStatus == 0;
            BindData();
        }

        private void BindData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            if (DJ_Type == 0)
                dc.Add("YWLX", EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.WL_JC));
            else
                dc.Add("YWLX", EnumHelper.GetDescriptionByName(typeof(EnumYWDJLX), EnumYWDJLX.BZX_JC));
            dc.Add("ZT", this.DjStatus.ToString());
            if (!string.IsNullOrEmpty(tbBH.Text.Trim()))
                dc.Add("BH", tbBH.Text.Trim());
            if (!string.IsNullOrEmpty(ddlDepartments.SelectedValue))
                dc.Add("Department", ddlDepartments.SelectedValue);
            if (!string.IsNullOrEmpty(ddlRSR.SelectedValue))
                dc.Add("Staff", ddlRSR.SelectedValue);
            if (!string.IsNullOrEmpty(txtS_Time.Text.Trim()))
                dc.Add("S_Time", txtS_Time.Text.Trim());
            if (!string.IsNullOrEmpty(txtE_Time.Text.Trim()))
                dc.Add("E_Time", txtE_Time.Text.Trim());
            using (JCCDBLL xBLL = new JCCDBLL())
            {
                int RecordCount = 0;
                List<vJCCD> itemLists = xBLL.GetJCCD(dc, "RQ desc", this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out RecordCount);
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
                this.AspNetPager1.RecordCount = RecordCount;
                this.AspNetPager1.Visible = RecordCount > 0;
                panMsg.Visible = !this.AspNetPager1.Visible;
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string ID = DataBinder.Eval(e.Row.DataItem, "ID").ToString();
                string ZT = DataBinder.Eval(e.Row.DataItem, "ZT").ToString();

                ImageButton btnEdit = (ImageButton)(e.Row.FindControl("btnEdit"));
                ImageButton btnConfirm = (ImageButton)(e.Row.FindControl("btnConfirm"));
                ImageButton btnDelete = (ImageButton)(e.Row.FindControl("btnDelete"));

                Label lblZT = ((Label)(e.Row.FindControl("lblZT")));
                if (int.Parse(ZT) == 0)
                {
                    lblZT.Text = "待确认";
                    lblZT.ForeColor = System.Drawing.Color.Red;
                    btnConfirm.Visible = true;
                    btnDelete.Visible = true;
                    btnEdit.Visible = true;
                    btnEdit.PostBackUrl = string.Format("{0}User/{1}?id={2}", AppPath, Edit_URL, ID);
                }
                else
                {
                    lblZT.ForeColor = System.Drawing.Color.Green;
                    lblZT.Text = "已确认";
                    btnConfirm.Visible = false;
                    btnDelete.Visible = false;
                    btnEdit.Visible = false;
                }
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Guid id = Guid.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
            using (JCCDBLL xBLL = new JCCDBLL())
            {
                if (xBLL.JcdConfirm(id))
                    ShowMsg("确认成功");
                else
                    ShowMsg("确认失败");
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            using (JCCDBLL xBLL = new JCCDBLL())
            {
                if (xBLL.Deletes("ID", id))
                    ShowMessage("删除成功");
                else
                    ShowMessage("删除失败");
            }
            BindData();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
    }
}